package com.ruoyi.web.controller.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ExpenseGroup;
import com.ruoyi.system.domain.UserModel;
import com.ruoyi.system.mapper.SysOrgPersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hnb
 * @Date 2025/06/10
 * @Description 德晨对接金蝶
 **/


@RestController
@RequestMapping("/dechenkingdee")
public class DechenJindieController extends BaseController {

    @Autowired
    private SysOrgPersonMapper sysOrgPersonMapper;

    // 新增客户端创建方法
    private K3CloudApi createNK3CloudClient() {
        IdentifyInfo iden = new IdentifyInfo();
        iden.setUserName("administrator");
        iden.setAppId("316325_W1bDW6ko5km/SVUK4/TLTYTKyMx+QOOG");
        iden.setdCID("682ede3bcfb84e");
        iden.setAppSecret("48c6fd30d14642ee9f5c5f5d58ff9da1");
        iden.setlCID(2052);
        iden.setServerUrl("http://218.59.183.210:9999/k3cloud/");
        return new K3CloudApi(iden);
    }
    private K3CloudApi createWK3CloudClient() {
        IdentifyInfo iden = new IdentifyInfo();
        iden.setUserName("administrator");
        iden.setAppId("316327_W/5rXyGPznHa74XG1fSMV/8sQjX66PqK");
        iden.setdCID("684ccbe1be9efe");
        iden.setAppSecret("701c64f2e38945419c21bab8b6e3b3bd");
        iden.setlCID(2052);
        iden.setServerUrl("http://218.59.183.210:9998/k3cloud/");
        return new K3CloudApi(iden);
    }

    @Log(title = "其他应付单-内账", businessType = BusinessType.OTHER)
    @PostMapping("/otherAccountsn")
    public AjaxResult otherAccountsn(@RequestBody Map<String,Object> map) {
        K3CloudApi client = createNK3CloudClient();  // 修改为调用新方法
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AP_OtherPayable";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
    @Log(title = "其他应付单-外账", businessType = BusinessType.OTHER)
    @PostMapping("/otherAccountsw")
    public AjaxResult otherAccountsw(@RequestBody Map<String,Object> map) {
        K3CloudApi client = createWK3CloudClient();  // 修改为调用新方法
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AP_OtherPayable";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 付款单
     * @return
     */
    @Log(title = "付款单-内账", businessType = BusinessType.OTHER)
    @PostMapping("/paymentOrdern")
    public AjaxResult paymentOrdern(@RequestBody Map<String,Object> map){
        K3CloudApi client = createNK3CloudClient();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AP_PAYBILL";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    @Log(title = "付款单-外账", businessType = BusinessType.OTHER)
    @PostMapping("/paymentOrderw")
    public AjaxResult paymentOrderw(@RequestBody Map<String,Object> map){
        K3CloudApi client = createWK3CloudClient();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AP_PAYBILL";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 收款单
     * @return
     */
    @Log(title = "收款单-内账", businessType = BusinessType.OTHER)
    @PostMapping("/receivePaymentn")
    public AjaxResult receivePaymentn(@RequestBody Map<String,Object> map){
        K3CloudApi client = createNK3CloudClient();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AR_RECEIVEBILL";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    @Log(title = "收款单-外账", businessType = BusinessType.OTHER)
    @PostMapping("/receivePaymentw")
    public AjaxResult receivePaymentw(@RequestBody Map<String,Object> map){
        K3CloudApi client = createWK3CloudClient();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AR_RECEIVEBILL";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 出库申请
     * @return
     */
    @Log(title = "出库申请单-内账", businessType = BusinessType.OTHER)
    @PostMapping("/outboundApplicationn")
    public AjaxResult outboundApplicationn(@RequestBody Map<String,Object> map){
        K3CloudApi client = createNK3CloudClient();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "STK_OutStockApply";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    @Log(title = "出库申请单-外账", businessType = BusinessType.OTHER)
    @PostMapping("/outboundApplicationw")
    public AjaxResult outboundApplicationw(@RequestBody Map<String,Object> map){
        K3CloudApi client = createWK3CloudClient();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "STK_OutStockApply";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 获取单据类型 BOS_BillType
     * @return
     */
    @Log(title = "获取单据类型", businessType = BusinessType.OTHER)
    @PostMapping("/getObtainDocument")
    public AjaxResult getObtainDocument(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        // 检查 map 中是否包含 FilterString 字段
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            // 记录开始时间
            long startTime = System.currentTimeMillis();
            // 调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            // 记录结束时间并计算耗时
            long endTime = System.currentTimeMillis();
            long duration = endTime - startTime;
            System.out.println("getObtainDocument接口调用耗时: " + duration + "毫秒");

            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                userModel.setFBaseProperty3((String)list.get(2));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 获取单据类型 BOS_BillType
     * @return
     */
    @Log(title = "获取单据类型-出库申请单", businessType = BusinessType.OTHER)
    @PostMapping("/getBosBillTypeu")
    public AjaxResult getBosBillType(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);

        // 检查 map 中是否包含 FilterString 字段
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }

        String jsonData = JSONObject.toJSONString(map);
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Map<String, Object>> listResult = null; // 修改为List<Map>类型
        try {
            //调用接口
            resultJson = client.billQuery(jsonData);
            ObjectMapper objectMapper = new ObjectMapper();
            listResult = objectMapper.readValue(resultJson, new TypeReference<List<Map<String, Object>>>(){});
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(listResult);
    }

    /**
     * 获取供应商 BD_Supplier
     * @return
     */
    @Log(title = "获取供应商", businessType = BusinessType.OTHER)
    @PostMapping("/getSupplier")
    public AjaxResult getSupplier(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        // 检查 map 中是否包含 FilterString 字段
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BD_Supplier\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }
    /**
     * 获取客户 BD_Customer
     * @return
     */
    @Log(title = "获取客户", businessType = BusinessType.OTHER)
    @PostMapping("/getCustomer")
    public AjaxResult getCustomer(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BD_Customer\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取员工 BD_Empinfo
     * @return
     */
    @Log(title = "获取员工", businessType = BusinessType.OTHER)
    @PostMapping("/getBdEmpinfo")
    public AjaxResult getBdEmpinfo(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BD_Empinfo\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取部门 BD_Department
     * @return
     */
    @Log(title = "获取部门", businessType = BusinessType.OTHER)
    @PostMapping("/getBdDepartment")
    public AjaxResult getBdDepartment(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BD_Department\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取其他往来单位 FIN_OTHERS
     * @return
     */
    @Log(title = "获取其他往来单位", businessType = BusinessType.OTHER)
    @PostMapping("/getFinOthers")
    public AjaxResult getFinOthers(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"FIN_OTHERS\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取组织机构
     * @return
     */
    @Log(title = "获取组织机构", businessType = BusinessType.OTHER)
    @PostMapping("/getORGOrganizations")
    public AjaxResult getORGOrganizations(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"ORG_Organizations\",\n" +
//                "    \"FieldKeys\": \"FNumber,FName\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取费用项目 BD_Expense
     * @return
     */
    @Log(title = "获取费用项目", businessType = BusinessType.OTHER)
    @PostMapping("/getBdExpense")
    public AjaxResult getBdExpense(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BD_Expense\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber,FGroup\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                userModel.setCount((Integer) list.get(2));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }
    /**
     * 获取费用项目 BD_Expense
     * @return
     */
    @Log(title = "获取费用项目分组查询", businessType = BusinessType.OTHER)
    @PostMapping("/getBdExpenseGroup")
    public AjaxResult getBdExpenseGroup(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
        K3CloudApi client = new K3CloudApi();
        List<ExpenseGroup> resultList = new ArrayList<>();
        try{
            String resultJson = client.queryGroupInfo(jsonData);
            Gson gson = new Gson();

            // 使用TypeToken解析完整JSON结构
            Map<String, Object> resultMap = gson.fromJson(resultJson,
                new TypeToken<Map<String, Map<String, Object>>>(){}.getType());

            // 逐层解析数据结构
            Map<String, Object> result = (Map<String, Object>) resultMap.get("Result");
            List<Map<String, Object>> needReturnData =
                (List<Map<String, Object>>) result.get("NeedReturnData");

            for (Map<String, Object> data : needReturnData) {
                // 使用Gson直接转换Map到ExpenseGroup对象
                ExpenseGroup group = gson.fromJson(
                    gson.toJsonTree(data).getAsJsonObject(),
                    ExpenseGroup.class
                );
                resultList.add(group);
            }

        } catch (Exception e) {
            return AjaxResult.error("数据解析失败: " + e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取在建工程 BAS_PreBaseDataThree
     * @return
     */
    @Log(title = "获取在建工程", businessType = BusinessType.OTHER)
    @PostMapping("/getPreBaseDataThree")
    public AjaxResult getPreBaseDataThree(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BAS_PreBaseDataThree\",\n" +
//                "    \"FieldKeys\": \"FNumber,FName\",\n" +
//                "    \" FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取安全投入项目 BOS_ASSISTANTDATA_DETAIL
     * @return
     */
    @Log(title = "获取安全投入项目", businessType = BusinessType.OTHER)
    @PostMapping("/getSafetyInvestment")
    public AjaxResult getSafetyInvestment(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BOS_ASSISTANTDATA_DETAIL\",\n" +
//                "    \"FieldKeys\": \"FNumber, FDataValue\",\n" +
//                "    \"FilterString\": [{\"FieldName\":\"FId.FNumber\",\"Compare\":\"67\",\"Value\":\"AQTRXM\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}," +
//                "{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 获取辅助资料-获取研发项目  BOS_ASSISTANTDATA_DETAIL
     * @return
     */
    @Log(title = "获取研发项目", businessType = BusinessType.OTHER)
    @PostMapping("/getBosAssistantata")
    public AjaxResult getBosAssistantata(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        if (map.containsKey("FilterString")) {
            List<Map<String, Object>> filterList = (List<Map<String, Object>>) map.get("FilterString");
            // 移除包含 "FUseOrgId.FNumber" 的过滤条件
            filterList.removeIf(filter -> "FUseOrgId.FNumber".equals(filter.get("FieldName")));
        }
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BOS_ASSISTANTDATA_DETAIL\",\n" +
//                "    \"FieldKeys\": \"FNumber, FDataValue\",\n" +
//                "    \"FilterString\": [{\"FieldName\":\"FId.FNumber\",\"Compare\":\"67\",\"Value\":\"YFXM\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}," +
//                "{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取结算方式  BD_SETTLETYPE
     * @return
     */
    @Log(title = "获取结算方式", businessType = BusinessType.OTHER)
    @PostMapping("/getBdSettletype")
    public AjaxResult getBdSettletype(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BD_SETTLETYPE\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \"FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取付款用途  CN_RECPAYPURPOSE
     * @return
     */
    @Log(title = "获取付款用途", businessType = BusinessType.OTHER)
    @PostMapping("/getRecpaypurpose")
    public AjaxResult getRecpaypurpose(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"CN_RECPAYPURPOSE\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \"FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }
    /**
     * 获取银行账号  CN_BANKACNT
     * @return
     */
    @Log(title = "获取银行账号", businessType = BusinessType.OTHER)
    @PostMapping("/getBankacnt")
    public AjaxResult getBankacnt(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"CN_BANKACNT\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \"FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取物料编码 BD_MATERIAL
     * @return
     */
    @Log(title = "获取物料编码", businessType = BusinessType.OTHER)
    @PostMapping("/getBdMaterial")
    public TableDataInfo getBdMaterial(@RequestBody Map<String,Object> map){
        Map<String,Object> map2 = new HashMap<>(map);
        if(StringUtils.isNotEmpty(String.valueOf(map.get("fName")))){
            extracted(map,"FName",17,String.valueOf(map.get("fName")));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("fNumber")))){
            extracted(map, "FNumber",17,String.valueOf(map.get("fNumber")));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("fbaseProperty3")))){
            extracted(map, "FSpecification",17,String.valueOf(map.get("fbaseProperty3")));
        }
        map.remove( "fName");
        map.remove( "fNumber");
        map.remove( "fbaseProperty3");
        if(StringUtils.isNotEmpty(String.valueOf(map.get("StartRow")))&&StringUtils.isNotEmpty(String.valueOf(map.get("Limit")))){
            int row = Integer.parseInt(String.valueOf(map.get("StartRow")));
            int  limit = Integer.parseInt(String.valueOf(map.get("Limit")));
            map.put( "StartRow",(row-1)*limit);
        }
        map = addFilterStringToMap(map);
        String jsonData = JSONObject.toJSONString(map);
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        Long countTotal = null;
        List<Object> resultList = new ArrayList<>();
        try {
            // 记录开始时间
            long startTime = System.currentTimeMillis();
            // 调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            // 记录结束时间并计算耗时
            long endTime = System.currentTimeMillis();
            long duration = endTime - startTime;
            System.out.println("getBdMaterial接口调用耗时: " + duration + "毫秒");
            map.put("FieldKeys","count(1)");
            map.put("Limit",0);
            map.remove( "OrderString");
            jsonData = JSONObject.toJSONString(map);
            countTotal = Long.parseLong(client.executeBillQuery(jsonData).get(0).get(0).toString());
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                userModel.setFBaseProperty3((String)list.get(2));
                userModel.setFdUnitName((String)list.get(3));
                userModel.setFdUnitNumber((String)list.get(4));
                userModel.setFMaterial((String)list.get(5));
                userModel.setFExecutionsTandard((String)list.get(6));
                userModel.setFCode((String)list.get(7));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return null;
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(resultList);
        rspData.setTotal(countTotal);
        return rspData;
    }

    //compare 17:包含；105
    private static void extracted(Map<String, Object> map,String key,int compare,String value) {
        // 定义要添加的 FilterString 内容
        List<Map<String, Object>> newFilterString = new ArrayList<>();
        Map<String, Object> filter1 = new HashMap<>();
        filter1.put("FieldName", key);
        filter1.put("Compare", compare);
        filter1.put("Value", value);
        filter1.put("Left", "");
        filter1.put("Right", "");
        filter1.put("Logic", "0");
        newFilterString.add(filter1);

        // 检查 map 中是否已经存在 FilterString 属性
        if (map.containsKey("FilterString")) {
            Object existingFilterStringObj = map.get("FilterString");
            if (existingFilterStringObj instanceof List) {
                @SuppressWarnings("unchecked")
                List<Map<String, Object>> existingFilterString = (List<Map<String, Object>>) existingFilterStringObj;
                // 合并新旧 FilterString 列表
                existingFilterString.addAll(newFilterString);
            } else {
                // 如果不是列表类型，直接替换为新的 FilterString
                map.put("FilterString", newFilterString);
            }
        }else {
            // 若不存在，直接添加
            map.put("FilterString", newFilterString);
        }
    }

    /**
     * 获取车辆信息 BAS_PreBaseDataTwo
     * @return
     */
    @Log(title = "获取车辆信息", businessType = BusinessType.OTHER)
    @PostMapping("/getPreBaseDataTwo")
    public AjaxResult getPreBaseDataTwo(@RequestBody Map<String,Object> map){
        map = addFilterStringToMap(map);
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\n" +
//                "    \"FormId\": \"BAS_PreBaseDataTwo\",\n" +
//                "    \"FieldKeys\": \"FName,FNumber\",\n" +
//                "    \"FilterString\": [{\"FieldName\":\"FDocumentStatus\",\"Compare\":\"105\",\"Value\":\"C\",\"Left\":\"\",\"Right\":\"\",\"Logic\":0}," +
//                "{\"FieldName\":\"FForbidStatus\",\"Compare\":\"105\",\"Value\":\"A\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}],\n" +
//                "    \"OrderString\": \"\",\n" +
//                "    \"TopRowCount\": 0,\n" +
//                "    \"StartRow\": 0,\n" +
//                "    \"Limit\": 2000,\n" +
//                "    \"SubSystemId\": \"\"\n" +
//                "}";
        K3CloudApi client = new K3CloudApi();
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }
    // 封装添加 FilterString 的方法
    private Map<String, Object> addFilterStringToMap(Map<String, Object> map) {
        // 定义要添加的 FilterString 内容
        List<Map<String, Object>> newFilterString = new ArrayList<>();
        Map<String, Object> filter1 = new HashMap<>();
        filter1.put("FieldName", "FForbidStatus");
        filter1.put("Compare", 105);
        filter1.put("Value", "A");
        filter1.put("Left", "");
        filter1.put("Right", "");
        filter1.put("Logic", "0");
        newFilterString.add(filter1);

        Map<String, Object> filter2 = new HashMap<>();
        filter2.put("FieldName", "FDocumentStatus");
        filter2.put("Compare", 105);
        filter2.put("Value", "C");
        filter2.put("Left", "");
        filter2.put("Right", "");
        filter2.put("Logic", "0");
        newFilterString.add(filter2);

        Map<String, Object> filter3 = new HashMap<>();
        filter3.put("FieldName", "FUseOrgId.FNumber");
        filter3.put("Compare", 67);
        filter3.put("Value", "101");
        filter3.put("Left", "");
        filter3.put("Right", "");
        filter3.put("Logic", "0");
        newFilterString.add(filter3);

        // 检查 map 中是否已经存在 FilterString 属性
        if (map.containsKey("FilterString")) {
            Object existingFilterStringObj = map.get("FilterString");
            if (existingFilterStringObj instanceof List) {
                @SuppressWarnings("unchecked")
                List<Map<String, Object>> existingFilterString = (List<Map<String, Object>>) existingFilterStringObj;
                // 合并新旧 FilterString 列表
                existingFilterString.addAll(newFilterString);
            } else {
                // 如果不是列表类型，直接替换为新的 FilterString
                map.put("FilterString", newFilterString);
            }
        } else {
            // 若不存在，直接添加
            map.put("FilterString", newFilterString);
        }
        return map;
    }

}
